<?php
class FetchModel extends Model {
    /**
     * Class CurlMulit 多线程采集
     */
    function remote($tmpurls, $reffer = null, $header = true,$charset=null) {
        $urls = array();
        if ($tmpurls && !is_array($tmpurls)) {
            $urls[] = $tmpurls;
        } else if (is_array($tmpurls)) {
            $urls = $tmpurls;
        } else {
            return false;
        }

        $user_agent = "Mozilla/5.0 (compatible; Baiduspider/2.0);+http://www.baidu.com/search/spider.html"; //来路

        $curl = $text = array();
        $handle = curl_multi_init();
        foreach ($urls as $k => $v) {
//         $nurl[$k]= preg_replace('~([^:\/\.]+)~ei', "rawurlencode('\\1')", $v);
            $nurl[$k] = $v;
            $curl[$k] = curl_init($nurl[$k]);
            $ip = $this->Rand_IP();
            curl_setopt($curl[$k], CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:' . $ip, 'CLIENT-IP:' . $ip));

            curl_setopt($curl[$k], CURLOPT_HEADER, $header);
            curl_setopt($curl[$k], CURLOPT_RETURNTRANSFER, true);
            curl_setopt($curl[$k], CURLOPT_FOLLOWLOCATION, false);
            curl_setopt($curl[$k], CURLOPT_NOBODY, false);
            if ($reffer)
                curl_setopt($curl[$k], CURLOPT_REFERER, $reffer); //来路地址
            curl_setopt($curl[$k], CURLOPT_USERAGENT, $user_agent);
            curl_setopt($curl[$k], CURLOPT_TIMEOUT, 10); //过期时间
            curl_multi_add_handle($handle, $curl[$k]);
        }

        $active = null;
        do {
            $mrc = curl_multi_exec($handle, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);

        while ($active && $mrc == CURLM_OK) {
            if (curl_multi_select($handle) != -1) {
                do {
                    $mrc = curl_multi_exec($handle, $active);
                } while ($mrc == CURLM_CALL_MULTI_PERFORM);
            }
        }

        foreach ($curl as $k => $v) {
            if (curl_error($curl[$k]) == "") {
                if($charset){
                    $texttmp = (string) curl_multi_getcontent($curl[$k]);
                    $text[$k] = mb_convert_encoding($texttmp, "utf-8",$charset);
                }else
                    $text[$k] = (string) curl_multi_getcontent($curl[$k]);

            }
            curl_multi_remove_handle($handle, $curl[$k]);
            curl_close($curl[$k]);
        }
        curl_multi_close($handle);
        return $text;
    }

    //动态ip
    function Rand_IP() {
        $ip2id = round(rand(600000, 2550000) / 10000);
        $ip3id = round(rand(600000, 2550000) / 10000);
        $ip4id = round(rand(600000, 2550000) / 10000);
        $arr_1 = array("218", "218", "66", "66", "218", "218", "60", "60", "202", "204", "66", "66", "66", "59", "61", "60", "222", "221", "66", "59", "60", "60", "66", "218", "218", "62", "63", "64", "66", "66", "122", "211");
        $randarr = mt_rand(0, count($arr_1) - 1);
        $ip1id = $arr_1[$randarr];
        return $ip1id . "." . $ip2id . "." . $ip3id . "." . $ip4id;
    }

    function xml2data($xml,$array_url)
    {
        $model_fetch = D('Fetch');
        $model_xml = D('Xml');
        if($array_url['action']){
            preg_match_all('/<video><last>([\s\S]*?)<\/last><id>([0-9]+)<\/id><tid>([0-9]+)<\/tid><name><\!\[CDATA\[([\s\S]*?)\]\]><\/name><type>([\s\S]*?)<\/type><pic>([\s\S]*?)<\/pic><lang>([\s\S]*?)<\/lang><area>([\s\S]*?)<\/area><year>([\s\S]*?)<\/year><state>([\s\S]*?)<\/state><note><\!\[CDATA\[([\s\S]*?)\]\]><\/note><actor><\!\[CDATA\[([\s\S]*?)\]\]><\/actor><director><\!\[CDATA\[([\s\S]*?)\]\]><\/director><dl>([\s\S]*?)<\/dl><des><\!\[CDATA\[([\s\S]*?)\]\]><\/des><\/video>/',$xml,$vod_array);
        }else{
            preg_match_all('/<video><last>([\s\S]*?)<\/last><id>([0-9]+)<\/id><tid>([0-9]+)<\/tid><name><\!\[CDATA\[([\s\S]*?)\]\]><\/name><type>([\s\S]*?)<\/type><dt>([\s\S]*?)<\/dt><note><\!\[CDATA\[([\s\S]*?)\]\]><\/note><\/video>/',$xml,$vod_array);
        }

        //组合数据
        foreach($vod_array[1] as $key=>$value){
            $listvod = array();
            $listvod['vod_addtime'] = $value;
            $listvod['vod_id'] = $vod_array[2][$key];
            $listvod['vod_cid'] = intval(ff_bind_id($array_url['fid'].'_'.$vod_array[3][$key]));
            $listvod['vod_name'] = $vod_array[4][$key];
            $listvod['list_name'] = $vod_array[5][$key];
            //以下参数只在内容页才有
            $listvod['vod_pic'] = $vod_array[6][$key];
            $listvod['vod_language'] = $vod_array[7][$key];
            $listvod['vod_area'] = $vod_array[8][$key];
            $listvod['vod_year'] = $vod_array[9][$key];
            $listvod['vod_continu'] = $vod_array[10][$key];
            $listvod['vod_actor'] = htmlspecialchars_decode($vod_array[12][$key]);
            $listvod['vod_director'] = htmlspecialchars_decode($vod_array[13][$key]);
            $listvod['vod_content'] = htmlspecialchars_decode($vod_array[15][$key]);
            $listvod['vod_inputer'] = $array_url['fid'].'_'.$listvod['vod_id'];
            $listvod['vod_reurl'] = admin_ff_url_repalce($array_url['reurl']).$listvod['vod_id'];
            if($array_url['action']){
                $listvod['vod_play'] = $vod_array[14][$key];
                $listvod['vod_title'] = $vod_array[11][$key];
                preg_match_all('/<dd flag="([\s\S]*?)"><\!\[CDATA\[([\s\S]*?)\]\]><\/dd>/',$vod_array[14][$key],$url_arr);
                $listvod['vod_play'] = str_replace(array('百度影音','youku'),array('bdhd','yuku'),implode('$$$',$url_arr[1]));
                $listvod['vod_url'] = htmlspecialchars_decode($this->xml_url_replace(implode('$$$',$url_arr[2])));
            }else{
                $listvod['vod_play'] = $vod_array[6][$key];
                $listvod['vod_title'] = $vod_array[7][$key];
            }
            echo $model_xml->xml_insert($listvod,$listvod['vod_pic']);
        }

    }

    function json2data($json,$array_url)
    {
        $model_fetch = D('Fetch');
        $model_xml = D('Xml');
        $data = json_decode($json);
        foreach($data->data as $key=>$value){
            $array_vod = array();
            $array_vod['vod_id'] = $value->vod_id;
            $array_vod['vod_cid'] = intval(ff_bind_id($array_url['fid'].'_'.$value->vod_cid));
            $array_vod['list_name'] = $value->list_name;
            $array_vod['vod_name'] = $value->vod_name;
            $array_vod['vod_title'] = $value->vod_title;
            $array_vod['vod_continu'] = $value->vod_continu;
            $array_vod['vod_addtime'] = $value->vod_addtime;
            $array_vod['vod_pic'] = $value->vod_pic;
            $array_vod['vod_actor'] = $value->vod_actor;
            $array_vod['vod_director'] = $value->vod_director;
            $array_vod['vod_content'] = $value->vod_content;
            $array_vod['vod_inputer'] = $array_url['fid'].'_'.$array_vod['vod_id'];
            $array_vod['vod_reurl'] = str_replace('||','//',$value->vod_reurl);
            if(!$array_vod['vod_reurl']){
                $array_vod['vod_reurl'] = $array_url['reurl'].$array_vod['vod_id'];
            }
            $array_vod['vod_area'] = $value->vod_area;
            $array_vod['vod_year'] = $value->vod_year;
            $array_vod['vod_language'] = $value->vod_language;
            $array_vod['vod_language'] = $value->vod_language;
            $array_vod['vod_total'] = $value->vod_total;
            $array_vod['vod_isend'] = $value->vod_isend;
            $array_vod['vod_isfilm'] = $value->vod_isfilm;
            $array_vod['vod_filmtime'] = $value->vod_filmtime;
            $array_vod['vod_keywords'] = $value->vod_keywords;
            $array_vod['vod_play'] = $value->vod_play;
            $array_vod['vod_url'] = $value->vod_url;
            echo $model_xml->xml_insert($array_vod,$array_vod['vod_pic']);
        }
    }

    //将资源站A类型XML方式获取到的播放地址转化为飞飞的播放地址格式
    public function xml_url_replace($playurl){
        $array_url = array();
        $arr_ji = explode('#',str_replace('||','//',$playurl));
        foreach($arr_ji as $key=>$value){
            $urlji = explode('$',$value);
            if(count($urlji)==3){
                $array_url[$key] = $urlji[0].'$'.trim($urlji[1]);
            }else{
                $array_url[$key] = trim($urlji[0]);
            }
        }
        return implode(chr(13),$array_url);
    }
}